<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Journal extends CI_Controller {
	private $data;

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('process_journal');
		$this->load->model('process_data');


		$rs = $this->process_data->get_main_subjects();
		$mainSubjectsSelectList = array();
		foreach ($rs as $val){
			$mainSubjectsSelectList[$val['id']] = $val['name'];
		}
		$this->data['mainSubjectsSelectList'] = $mainSubjectsSelectList;

		$rs = $this->process_data->get_subjects();
		$subjectsSelectList = array();
		foreach ($rs as $val){
			$subjectsSelectList[$val['id']] = $val['name'];
		}
		$this->data['subjectsSelectList'] = $subjectsSelectList;
	}

	public function index($id = FALSE)
	{
		if($id === FALSE){
			echo "Select journal";
		}else{
			$this->show($id);
		}
	}

	public function show($id = FALSE)
	{
		if($id === FALSE){
			echo "Select journal";
		}else{
			$this->load->view('journal_show');
		}
	}

	public function publishnew()
	{
		//Check for login status if yes stay here
		if($this->session->userdata('email') != null && $this->session->userdata('logged_in') === TRUE){
			$data = $this->data;
			$data['userDetail'] = $this->process_data->usersFullDetails($this->session->userdata('email'));
			$this->load->view('journal_upload', $data);
		}else{
			//Else
			redirect(base_url().'register/newuser/uploadjournal', 'refresh');
		}
	}

	public function uploadjournal()
	{
		$data = $this->data;
		$this->process_journal->upload_journal();


			
		//Code to send mail to Admin, uploader
		//Mail to admin users
		$emailIds = $this->process_data->get_adminEmails();

		$this->load->library('email');

		$this->email->from('admin@gascian.com', 'Gascian');
		$this->email->to('admin@gascian.com',$this->input->post('author2email'),$this->input->post('author3email'));


		$this->email->subject('Email Admin Test');
		$this->email->message('Upload Journal in Gascian');

		$adminEmailId = array();
		if(is_array($emailIds))	{
			foreach ($emailIds as $emailRs)	{
				//Send mail to $emailRs['email']
				$adminEmailId[] = $emailRs['email'];
			}
		}
		$this->email->cc($adminEmailId);
		
		$this->email->send();

		redirect('myprofile/myuploads/');
		//$this->load->view('myprofile/myuploads/', $data);
	}

}

